package com.tyk.bookstore.back.product.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import com.tyk.bookstore.back.common.core.excel.ExcelManager;
import com.tyk.bookstore.back.common.core.model.page.PageVo;
import com.tyk.bookstore.back.common.core.model.result.R;
import com.tyk.bookstore.back.common.core.model.type.BinVo;
import com.tyk.bookstore.back.common.core.model.type.SuggestionQuery;
import com.tyk.bookstore.back.product.manager.excel.manager.ExcelConstant;
import com.tyk.bookstore.back.product.manager.excel.model.PublisherExcelVo;
import com.tyk.bookstore.back.product.model.query.PublisherAddQuery;
import com.tyk.bookstore.back.product.model.query.PublisherPageQuery;
import com.tyk.bookstore.back.product.model.query.PublisherUpdateQuery;
import com.tyk.bookstore.back.product.model.vo.PublisherPageVo;
import com.tyk.bookstore.back.product.model.vo.PublisherVo;
import com.tyk.bookstore.back.product.service.PublisherService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Set;


/**
 * @author tyk
 * @description 出版社管理
 */
@RequestMapping({"api/product/publisher"})
@Tag(name = "商品管理/出版社管理", description = "出版社管理")
@RestController
@RequiredArgsConstructor
public class PublisherController {
    private final PublisherService publisherService;
    private final ExcelManager<PublisherExcelVo> excelManager;


    @Operation(summary = "获取出版社详情", description = "获取出版社详情")
    @SaCheckPermission("product:publisher:get")
    @GetMapping({"{id}"})
    public R<PublisherVo> getById(
            @Schema(description = "出版社ID") @NotNull @Min(1)
            @PathVariable Long id
    ) {
        return R.ok(publisherService.getById(id));
    }


    @Operation(summary = "分页查询出版社", description = "分页查询出版社")
    @SaCheckPermission("product:publisher:page")
    @GetMapping({"page"})
    public R<PublisherPageVo> getPage(
            @Validated @ParameterObject PublisherPageQuery query,
            BindingResult br
    ) {
        if (br.hasErrors()) {
            return R.ok(PageVo.instance(PublisherPageVo.class));
        }
        return R.ok(publisherService.getPage(query));
    }


    @Operation(summary = "出版社搜索建议列表", description = "出版社搜索建议列表")
    @SaCheckPermission("product:publisher:suggest")
    @GetMapping({"suggest"})
    public R<List<BinVo>> getSuggestionList(
            @Validated @ParameterObject SuggestionQuery query
    ) {
        return R.ok(publisherService.getSuggestionList(query));
    }


    @Operation(summary = "获取出版社名称列表", description = "获取出版社名称列表")
    @SaCheckPermission("product:publisher:list")
    @GetMapping({"list"})
    public R<List<BinVo>> getNameVoList() {
        return R.ok(publisherService.getNameVoList());
    }


    @Operation(summary = "添加出版社", description = "添加出版社")
    @SaCheckPermission("product:publisher:add")
    @PostMapping
    public R<Object> add(
            @Validated @RequestBody PublisherAddQuery query
    ) {
        publisherService.add(query);
        return R.ok();
    }


    @Operation(summary = "更新出版社", description = "更新出版社")
    @SaCheckPermission("product:publisher:update")
    @PutMapping
    public R<Object> updateById(
            @Validated @RequestBody PublisherUpdateQuery query
    ) {
        publisherService.updateById(query);
        return R.ok();
    }


    @Operation(summary = "删除出版社", description = "删除出版社")
    @SaCheckPermission("product:publisher:delete")
    @DeleteMapping({"{id}"})
    public R<Object> deleteById(
            @Schema(description = "出版社ID") @NotNull @Min(1)
            @PathVariable Long id
    ) {
        publisherService.deleteById(id);
        return R.ok();
    }


    @Operation(summary = "批量删除出版社", description = "批量删除出版社")
    @SaCheckPermission("product:publisher:delete")
    @DeleteMapping
    public R<Object> deleteBatchByIdList(
            @Schema(description = "出版社ID列表") @NotNull @NotEmpty Set<Long> idList
    ) {
        publisherService.deleteBatchByIdSet(idList);
        return R.ok();
    }


    @Operation(summary = "导出Excel", description = "导出Excel")
    @SaCheckPermission("product:publisher:excel:download")
    @GetMapping("excel")
    public void downloadExcel(PublisherPageQuery query, HttpServletResponse resp) {
        excelManager.write(
                publisherService.getPublisherExcelVoList(query),
                ExcelConstant.PUBLISHER_FILE_NAME,
                ExcelConstant.PUBLISHER_SHEET,
                resp
        );
    }


    @Operation(summary = "导入Excel", description = "导入Excel")
    @SaCheckPermission("product:publisher:excel:upload")
    @PostMapping("excel")
    public R<Object> uploadExcel(MultipartFile file) {
        excelManager.save(
                file,
                publisherService::savePublisherExcelVoList
        );
        return R.ok();
    }

    @Operation(summary = "下载Excel模板", description = "下载Excel模板")
//    @SaCheckPermission("product:publisher:excel:template")
    @GetMapping("excel/template")
    public void excelTemplate(HttpServletResponse resp) {
        excelManager.template(
                ExcelConstant.PUBLISHER_TEMPLATE,
                List.of("名字", "描述"),
                resp
        );
    }
}
